package com.example.config;

import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.dangdang.ddframe.job.config.JobCoreConfiguration;
import com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration;
import com.dangdang.ddframe.job.event.rdb.JobEventRdbConfiguration;
import com.dangdang.ddframe.job.lite.api.JobScheduler;
import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration;
import com.dangdang.ddframe.job.lite.spring.api.SpringJobScheduler;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;
import com.example.job.PrintJob;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import javax.sql.DataSource;

/**
 * @author Shanks
 * @date 2020-05-29
 */
@Configuration
public class ElasticJobConfig {

    @Resource
    private ZookeeperRegistryCenter zookeeperRegistryCenter;

    @Resource(name = "elasticJobDataSource")
    private DataSource elasticJobDataSource;

    /**
     * 配置任务详细信息
     *
     * @param jobClass               作业类
     * @param cron                   作业启动时间的cron表达式
     * @param shardingTotalCount     作业分片总数
     * @param shardingItemParameters 作业分片子项参数
     * @return LiteJobConfiguration
     */
    private LiteJobConfiguration getLiteJobConfiguration(final Class<? extends SimpleJob> jobClass,
                                                         final String cron,
                                                         final int shardingTotalCount,
                                                         final String shardingItemParameters,
                                                         final String description) {
        JobCoreConfiguration jobCoreConfiguration = JobCoreConfiguration.newBuilder(jobClass.getName(), cron, shardingTotalCount)
                .description(description)
                .shardingItemParameters(shardingItemParameters)
                .build();
        SimpleJobConfiguration simpleJobConfiguration = new SimpleJobConfiguration(jobCoreConfiguration, jobClass.getCanonicalName());
        return LiteJobConfiguration.newBuilder(simpleJobConfiguration)
                .overwrite(true)
                .build();
    }

    @Bean(initMethod = "init")
    public JobScheduler printJobScheduler(final PrintJob printJob,
                                          @Value("${elastic-job.task.printJob.cron}") final String cron,
                                          @Value("${elastic-job.task.printJob.sharding-total-count}") final int shardingTotalCount,
                                          @Value("${elastic-job.task.printJob.sharding-item-parameters}") final String shardingItemParameters,
                                          @Value("${elastic-job.task.printJob.description}") final String description) {
        return new SpringJobScheduler(printJob, zookeeperRegistryCenter, getLiteJobConfiguration(printJob.getClass(), cron, shardingTotalCount, shardingItemParameters, description), new JobEventRdbConfiguration(elasticJobDataSource));
    }
}